library(caret)
library(rpart)
library(pROC)
load("model.Rdata")
load("../features/features_combined.Rdata")
source("../load_data.R")
test_features$STUDENTID<-NULL

# 使用每个模型预测结果
predictions_res <- lapply(results, function(model_info) {
  model <- model_info$model
  predict(model, newdata = test_features, type = "raw")
})
# 使用模型预测概率
predictions_prob <- lapply(results, function(model_info) {
  model <- model_info$model
  predict(model, newdata = test_features, type = "prob")
})

save(predictions_res,predictions_prob,
     file = "predictions.Rdata")

actuals <- factor(testBlockB$EfficientlyCompletedBlockB,
                  levels = c( FALSE,TRUE), 
                  labels = c( "False","True")) 

# 对每个模型的预测结果进行评估
for (model_name in names(predictions_res)) {
  cat("Evaluating model:", model_name, "\n")
  pred <- predictions_res[[model_name]]
  levels(pred) <- c("False", "True")
  conf_matrix <- confusionMatrix(pred, actuals)
  print(conf_matrix)
  # 使用模型预测的概率和实际类别来计算ROC曲线
  roc_result <- roc(actuals, predictions_prob[[model_name]][, "TRUE."])
  # 计算AUC
  auc_value <- auc(roc_result)
  print(auc_value)
}